(function () {
    var _inputCss = ['<style id="custom_input_style">',
                        '.custom_input_wrap{ width:100%; height:100%; position:fixed; left:0; top:0; right:0; bottom:0; z-index:100000;}',
                        '.custom_input_wrap > .custom_input_mask{ width:100%; height:100%; background:rgba(0, 0, 0, 0.5); position:absolute; left:0; top:0; right:0; bottom:0;}',
                        '.custom_input_wrap > .custom_input_main{ width:100%; height:1rem; background:#fff; position:fixed; left:0; bottom:0; right:0;}',
                        '.custom_input_wrap > .custom_input_main > .custom_input_text_wrap{ width:60%; height:0.66rem; border-radius:0.3rem; box-shadow:0 0 2px 0 #c7c7cc inset; position:absolute; left:5%; bottom:0.15rem;}',
                        '.custom_input_wrap > .custom_input_main > .custom_input_text_wrap > .custom_input_text{ display:block; width:90%; height:0.66rem; line-height:0.66rem; font-family:"Microsoft Yahei"; font-size:0.34rem; background:none; border:none; outline:none; margin:0 auto;}',
                        '.custom_input_wrap > .custom_input_main > .custom_input_confirm{ width:20%; height:0.66rem; line-height:0.66rem; text-align:center; font-family:"Microsoft Yahei"; font-size:0.3rem; color:#fff; background:#00cc47; border-radius:0.3rem; position:absolute; right:5%; bottom:0.15rem;}',
                    '</style>'].join("");

    var _inputTpl = ['<div class="custom_input_wrap">',
                        '<div class="custom_input_mask"></div>',
                        '<div class="custom_input_main">',
                            '<div class="custom_input_text_wrap">',
                                '<input type="tel" name="" id="" class="custom_input_text" placeholder="请输入大于0的整数" value="{defaultValue}" maxlength="11" />',
                            '</div>',
                            '<span class="custom_input_confirm">确定</span>',
                        '</div>',
                    '</div>'].join("");

    function CustomInput() {
        this.defaultValue      = null;
        this.elementWrap       = null;
        this.elementInput      = null;
        this.elementBtnConfirm = null;
        this.elementMask       = null;

        this.inputValue        = null;

        $(_inputCss).appendTo("head");

        viewer.register("custom.input.show", this.show.bind(this));
    }

    var _proto = CustomInput.prototype;

    _proto.clickEvent = "click";
    _proto.isMobile = function(){
        return "ontouchend" in document;
    }
    _proto.setClickEvent = function () {
        if(this.isMobile()){
            this.clickEvent = "tap";
        }else{
            this.clickEvent = "click";
        }
    }
    _proto.bindEvent = function () {
        var self = this;
        //输入框
        this.elementInput.on("focus", function(){
            if(this.value == "充值额度只能是大于0且为正整数"){
                $(this).val(self.defaultValue).removeAttr("style");
            }
        }).on("keydown", function(e){
            //如果是按下了Shift键，或者非数字、非方向键、非回车键、非退格键，则不允许输入
            if(e.shiftKey || !((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || (e.keyCode >= 37 && e.keyCode <= 40) || e.keyCode == 13 || e.keyCode == 8)){
                return false;
            }
        }).on("keyup", function (e) {
            var value = this.value;

            if(parseInt(value, 10) >= 100000){
                this.value = 100000;
                return;
            }

            self.inputValue = this.value;

            if(e.keyCode == 13){
                if(value.length == 0){
                    $(this).val("充值额度只能是大于0且为正整数").css({"color":"#f00","font-size":"0.24rem"});
                    setTimeout(function () {
                        $(this).select();
                    }.bind(this), 30);
                    return;
                }

                self.close("input");
            }
        }).on("blur", function (e) {
            var value = this.value;

            if(value.length == 0){
                $(this).val("充值额度只能是大于0且为正整数").css({"color":"#f00","font-size":"0.24rem"});
                setTimeout(function () {
                    $(this).select();
                }.bind(this), 30);
                return;
            }
        });

        this.elementBtnConfirm.on(this.clickEvent, function () {
            if(self.inputValue.length == 0){
                self.elementInput.val("充值额度只能是大于0且为正整数").css({"color":"#f00","font-size":"0.24rem"});
                setTimeout(function () {
                    $(this).select();
                }.bind(this), 30);
                return;
            }
            self.close("input");
        });

        this.elementMask.on(this.clickEvent, function (e) {
            self.close();
        });
    }

    _proto.show = function (defaultValue) {
        var _tpl = $(_inputTpl.replace(/{defaultValue}/, defaultValue));
        
        _tpl.appendTo("body");

        this.defaultValue      = defaultValue;
        this.inputValue        = defaultValue;

        this.elementWrap       = _tpl;
        this.elementInput      = _tpl.find(".custom_input_text");
        this.elementBtnConfirm = _tpl.find(".custom_input_confirm");
        this.elementMask       = _tpl.find(".custom_input_mask");

        this.bindEvent();

        this.elementInput.focus();
        setTimeout(function () {
            this.elementInput[0].select();
        }.bind(this), 30);
    }
    _proto.close = function (action) {
        if(action == "input"){
            viewer.publish("custom.input", this.inputValue);
        }

        this.elementWrap.remove();

        this.defaultValue      = null;
        this.elementWrap       = null;
        this.elementInput      = null;
        this.elementBtnConfirm = null;
        this.elementMask       = null;

        this.inputValue        = null;

    }

    new CustomInput();
})();